//package assignment1;

import java.io.IOException;
import java.net.ServerSocket;

public class P2PApp {

	/**
	 * @param args
	 */
	public static void main(String[] args) {

		int argc = args.length;
		int ID, myPort;
		int fileID;
		
		//TODO: Check number of args
		if (argc >= 2) {
			fileID = Integer.parseInt(args[0]);		// my share files ID
			ID = Integer.parseInt(args[1]);			// my ID
			myPort = Integer.parseInt(args[2]);		// my port
			
			P2PNode p2p;
			if (argc == 4) {						/* init client */
				String[] parts = args[3].split(":");
				p2p = new P2PNode(fileID, ID, myPort, parts[0], Integer.parseInt(parts[1]));
			}
			else {									/* init host */
				p2p = new P2PNode(fileID, ID, myPort);
				try {
					p2p.InitServer = new ServerSocket(p2p.MyPort, 5, null );
					p2p.ConnTable.put(p2p.MyID, p2p.MyFullAddr);
					
				} catch (IOException e) {
					System.out.println(e);
					System.out.println("Could not listen on port " + p2p.MyPort);
					System.exit(-1);
				}
			}
			p2p.launch();
		}

	}

}
